<?php  
namespace app\library\admin;
use app\admin\controller\Admin;
use app\library\model\Area as AreaModel;
use app\library\model\Seat as SeatModel;
use app\library\model\Reservation as ReservationModel;
use app\common\util\applet\Generate;
//座位控制器
class Seat extends Admin{

	public function index(){
        $area = new AreaModel();
        $reservation = new ReservationModel();
		$this->assign('tab_type', 3);
        if ($this->request->isAjax()) {
            $where = $data = [];
            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 15);
            $list = SeatModel::where($where)->page($page)->limit($limit)->order('areaid,seatNum asc')->select();
            $areaName = $area->getAllAreaName();
            foreach ($list as $key => $value) {
                foreach ($areaName as $k => $v) {
                    if ($value['areaid'] == $k) {
                        $list[$key]['areaname'] = $v;
                    }
                }
                switch ($value['seatType']) {
                    case '1': $seatTypeName = '硬座';break;
                    case '2': $seatTypeName = '软座';break;
                    case '3': $seatTypeName = '学生仓';break;
                    case '4': $seatTypeName = '交流间';break;
                    default:$seatTypeName = '暂无';; break;
                }
                $map['areaid'] = $value['areaid'];
                $map['seatNum'] = $value['seatNum'];
                $list[$key]['seatType'] = $seatTypeName;
                $list[$key]['dTotal'] = $reservation->dTotal($map);
                $list[$key]['wTotal'] = $reservation->wTotal($map);
                $list[$key]['mTotal'] = $reservation->mTotal($map);
                $list[$key]['yTotal'] = $reservation->yTotal($map);
                $list[$key]['total'] = $reservation->total($map);
            }
            $data['data'] = $list;
            $data['count'] = SeatModel::where($where)->count('id');
            $data['code'] = 0;
            $data['msg'] = '';
            return json($data);
        }
        return $this->fetch();
	}
    /**
     * 删除单个座位
     * @return mixed
     */
    public function delOne(){
        $id = input('param.id/a');
        $seat = new SeatModel();
        $reservation = new ReservationModel();
        if ($reservation->isReservation($id)) {
        	return $this->error('该区域座位还存在未完成的预约，不能删除');
        }
        if ($seat->del($id)) {
            return $this->success('删除成功。');
        }
        return $this->error($seat->getError());
    }
    /**
     * 删除区域所有位置
     * @return mixed
     */
    public function delAll(){
        $areaid = input('param.areaid');
        if (empty($areaid)) {
            return $this->error('参数不能为空');
        }
        $seat = new SeatModel();
        $seatArr = $seat->getSeatArr($areaid);
        $id = [];
        foreach ($seatArr as $key => $value) {
            $id[] = $value['id'];
        }
        if (empty($id)) {
            return $this->success('删除成功。');
        }
        $reservation = new ReservationModel();
        if ($reservation->isReservation($id)) {
            return $this->error('该区域座位还存在未完成的预约，不能删除');
        }
        if ($seat->del($id)) {
            return $this->success('删除成功。');
        }
        return $this->error($seat->getError());
    }
    /**
     * 查看二维码
     * @return 
     */
    public function code() {
        $map = input('param.');
        $generate = new Generate();
        $code = $generate->code($map);
        if (empty($code['data'])) {
            $code['data'] = '';
        }
        $this->assign('code',$code['data']);
        $this->assign('codename',$map['areaname'].'_'.$map['seatNum']);
        $this->view->engine->layout(false);
        return $this->fetch();
    }
    /**
     * 更新二维码
     * @return 
     */
    public function delCode(){
        $map = input('param.');
        $res = db('code')->where($map)->delete();
        if ($res) {
            return $this->success('删除成功，请重新生成二维码');
        }
        return $this->error('删除失败，请先生成二维码');
    }
}
?>